package com.github.davidfantasy.mybatisplus.generatorui.controller;

import com.baomidou.mybatisplus.generator.config.po.TableField;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.github.davidfantasy.mybatisplus.generatorui.configurer.GeneratorConfig;
import com.github.davidfantasy.mybatisplus.generatorui.common.Result;
import com.github.davidfantasy.mybatisplus.generatorui.common.ResultGenerator;
import com.github.davidfantasy.mybatisplus.generatorui.desc.DescFinder;
import com.github.davidfantasy.mybatisplus.generatorui.desc.TableInfoParse;
import com.github.davidfantasy.mybatisplus.generatorui.dto.GenDtoFromSqlReq;
import com.github.davidfantasy.mybatisplus.generatorui.dto.GenDtoPlus;
import com.github.davidfantasy.mybatisplus.generatorui.dto.SqlDTO;
import com.github.davidfantasy.mybatisplus.generatorui.service.SqlGeneratorService;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.JSQLParserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/sql")
public class SqlGeneratorController {

    @Autowired
    private SqlGeneratorService sqlGeneratorService;

    @Autowired
    private GeneratorConfig generatorConfig;

    @Autowired
    private TableInfoParse tableInfoParse;

    @GetMapping("/basepackage")
    public Result getBasepackage() {
        return ResultGenerator.genSuccessResult(generatorConfig.getBasePackage());
    }


    @PostMapping("/gen-mapper-method")
    public Result genMapperMethodFromSQL(@RequestBody GenDtoFromSqlReq params) throws Exception {
        sqlGeneratorService.genMapperMethod(params);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/genplus")
    public Result genMapperMethodFromSQLPlus(@RequestBody GenDtoPlus params) throws Exception {
        sqlGeneratorService.genMapperMethodPlus(params);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/getDesc")
    public Result getSqlDesc(@RequestBody SqlDTO sqlDTO) {
        DescFinder descFinder = new DescFinder();
        String sql = sqlDTO.getSql();
        try {
            List<DescFinder.ParseResult> parse = descFinder.parse(sql,tableInfoParse);

            List<String> allTable = descFinder.getAllTable();

            List<TableInfo> tablesDesc = tableInfoParse.getTablesDesc(allTable);

            for (DescFinder.ParseResult parseResult : parse) {
                List<DescFinder.Col> cols = parseResult.getCols();
                if(cols==null){
                   continue;
                }
                String oper = parseResult.getOper();



                List<String> descs = new LinkedList<>();
                for (DescFinder.Col col : cols) {
                    String tableName = col.getTableName();
                    String colName = col.getColName();
                    //查询注释
                    for (TableInfo tableInfo : tablesDesc) {
                        String tName = tableInfo.getName();
                        if(tName.equals(tableName)){
                            for (TableField field : tableInfo.getFields()) {
                                String name = field.getName();
                                if(name.equals(colName)){
                                    parseResult.setDataType(field.getColumnType().getType());
                                    parseResult.setImportPackage(field.getColumnType().getPkg());
                                    descs.add(field.getComment());
                                }
                            }
                        }
                    }
                }

                if(oper!=null){
                    if (oper.contains("求和")||oper.contains("计数")) {
                        parseResult.setDataType("Long");
                    }else{
                        parseResult.setDataType("String");
                    }
                }

                String comment = oper==null?String.join(",",descs):oper+"("+String.join("+",descs)+")";
                parseResult.setDesc(comment);
            }

            return ResultGenerator.genSuccessResult(parse);

        } catch (JSQLParserException e) {
            log.error("sql解析失败",e);
            ResultGenerator.genFailResult("sql解析失败");
        }

        return ResultGenerator.genSuccessResult("失败");
    }


}
